#include "../source/http/http_parser.h"
#include "../source/log.h"
#include <ios>
#include <sstream>

const char test_request_data[] = "POST / HTTP/1.0\r\n"
                                 "Host: www.baidu.top\r\n"
                                 "Content-Length: 10\r\n\r\n"
                                 "1234567890";

void test_request()
{
    std::string tmp = test_request_data;
    MYHTTP::HttpRequestParser parser;
    size_t s = parser.execute(tmp.data(), tmp.size());
    log_debug("\nexecutes ret:{}, \nhas_error:{}\ntotal:{}\nContent-Length:{}", 
        s, parser.hasError(), tmp.size(), parser.getContentLength());
    log_debug("==============================");
    log_debug("\n{}", parser.getData()->toString());
    log_debug("==============================");
    
    tmp.resize(tmp.size() - s);
    std::ostringstream oss;
    oss << std::hex << "0x" << (int)parser.getData()->getVersion();
    log_debug("body:{} vesion:{}", tmp, oss.str());
}

const char test_response_data[] = "HTTP/1.1 200 OK\r\n"
        "Date: Tue, 04 Jun 2024 15:43:56 GMT\r\n"
        "Server: Apache\r\n"
        "Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT\r\n"
        "ETag: \"51-47cf7e6ee8400\"\r\n"
        "Accept-Ranges: bytes\r\n"
        "Content-Length: 81\r\n"
        "Cache-Control: max-age=86400\r\n"
        "Expires: Wed, 05 Jun 2024 15:43:56 GMT\r\n"
        "Connection: Close\r\n"
        "Content-Type: text/html\r\n\r\n"
        "<html>\r\n"
        "<meta http-equiv=\"refresh\" content=\"0;url=http://www.baidu.com/\">\r\n"
        "</html>\r\n";

void test_response()
{
    std::string tmp = test_response_data;
    MYHTTP::HttpResponseParser parser;
    size_t s = parser.execute(tmp.data(), tmp.size(), true);
    log_info("\nexecutes ret:{}, \nhas_error:{}\nis_finished:{}\ntotal:{}\nContent-Length:{}\ntemp[s]:{}", 
        s, parser.hasError(), parser.isFinished(), tmp.size(), parser.getContentLength(), tmp[s]);
    log_info("==============================");
    log_info("\n{}", parser.getData()->toString());
    log_info("==============================");
    
    tmp.resize(tmp.size() - s);
    std::ostringstream oss;
    oss << std::hex << "0x" << (int)parser.getData()->getVersion();
    log_info("\nbody:{} vesion:{}", tmp, oss.str());
}


int main(void)
{
    test_request();
    test_response();

    return 0;
}

